package com.fruitshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fruitshop.dto.CategoryDTO;
import com.fruitshop.dto.ProductDTO;
import com.fruitshop.entity.Category;
import com.fruitshop.entity.Product;
import com.fruitshop.entity.ProductDetail;
import com.fruitshop.exception.BusinessException;
import com.fruitshop.mapper.CategoryMapper;
import com.fruitshop.mapper.ProductDetailMapper;
import com.fruitshop.mapper.ProductMapper;
import com.fruitshop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品服务实现类
 */
@Service
public class ProductServiceImpl implements ProductService {
    
    @Autowired
    private CategoryMapper categoryMapper;
    
    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private ProductDetailMapper productDetailMapper;
    
    @Override
    public List<CategoryDTO> getAllCategories() {
        // 使用自定义查询方法获取所有分类
        List<Category> categories = categoryMapper.findAllActiveCategories();
        return categories.stream()
                .map(this::convertToCategoryDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<ProductDTO> getProductsByCategory(Integer categoryId) {
        List<Product> products;
        if (categoryId == null) {
            // 如果分类ID为空，返回所有商品
            QueryWrapper<Product> wrapper = new QueryWrapper<>();
            wrapper.eq("status", 1)
                  .eq("deleted", 0);
            products = productMapper.selectList(wrapper);
        } else {
            // 使用自定义查询方法获取分类下的商品
            products = productMapper.findByCategoryId(categoryId);
        }
        return products.stream()
                .map(this::convertToProductDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public ProductDetail getProductDetail(Integer detailId) {
        // 使用LambdaQueryWrapper构建查询条件
        LambdaQueryWrapper<ProductDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetail::getDetailId, detailId)
               .eq(ProductDetail::getStatus, 1)
               .eq(ProductDetail::getDeleted, 0);
        
        // 查询商品详情
        ProductDetail detail = productDetailMapper.selectOne(wrapper);
        if (detail == null) {
            throw new BusinessException("商品详情不存在");
        }
        
        return detail;
    }
    
    @Override
    public List<ProductDTO> searchProducts(String keyword) {
        // 使用自定义查询方法搜索商品
        List<Product> products = productMapper.searchByKeyword(keyword);
        return products.stream()
                .map(this::convertToProductDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<ProductDTO> getHotProducts(Integer limit) {
        if (limit == null || limit <= 0) {
            limit = 6; // 默认返回6个热销商品
        }
        
        List<Product> products = productMapper.findHotProducts(limit);
        return products.stream()
                .map(this::convertToProductDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将分类实体转换为DTO
     */
    private CategoryDTO convertToCategoryDTO(Category category) {
        CategoryDTO dto = new CategoryDTO();
        dto.setId(category.getId());
        dto.setTitle(category.getTitle());
        return dto;
    }
    
    /**
     * 将商品实体转换为DTO
     */
    private ProductDTO convertToProductDTO(Product product) {
        ProductDTO dto = new ProductDTO();
        dto.setId(product.getId());
        dto.setCategoryId(product.getCategoryId());
        dto.setTitle(product.getTitle());
        dto.setDescription(product.getDescription());
        
        // 返回不包含￥符号的纯数字价格
        dto.setPrice(product.getPrice().toString());
        
        dto.setUnit(product.getUnit());
        dto.setImg_url(product.getImgUrl());
        dto.setNumber(product.getNumber());
        return dto;
    }
} 